package com.xcity.game.worldboss.template;

import com.xcity.game.combat.CombatUtils;
import com.xcity.game.item.DropGroupTemplate;
import com.xcity.game.npc.NpcTeamTemplate;
import com.xcity.game.npc.NpcTemplate;

import naga.x.App;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;

/**
 * 世界boss
 * @author yang.li
 *
 */
@Excel(value = "world_boss.xlsx", sheet = 0)
public class WorldBossTemplate extends Template {

	/** 等级 */
	protected int level;

	/** npc战队id */
	protected int npcTeamId;
	protected NpcTeamTemplate npcTeam;

	/** 击杀奖励掉落组 */
	protected int killDropId;
	protected DropGroupTemplate killDrop;

	/** 战斗奖励掉落组 */
	protected int dropId;
	protected DropGroupTemplate drop;
	
	protected long hp = -1;

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getNpcTeamId() {
		return npcTeamId;
	}

	public void setNpcTeamId(int npcTeamId) {
		this.npcTeamId = npcTeamId;
	}

	public int getKillDropId() {
		return killDropId;
	}

	public void setKillDropId(int killDropId) {
		this.killDropId = killDropId;
	}

	public int getDropId() {
		return dropId;
	}

	public void setDropId(int dropId) {
		this.dropId = dropId;
	}

	public NpcTeamTemplate getNpcTeam() {
		return npcTeam != null ? npcTeam : (npcTeam = App.getApp().getServiceManager().get(TemplateService.class).get(NpcTeamTemplate.class, npcTeamId));
	}
	
	public DropGroupTemplate getKillDrop() {
		return killDrop != null ? killDrop : (killDrop = App.getApp().getServiceManager().get(TemplateService.class).get(DropGroupTemplate.class, killDropId));
	}
	
	public DropGroupTemplate getDrop() {
		return drop != null ? drop : (drop = App.getApp().getServiceManager().get(TemplateService.class).get(DropGroupTemplate.class, dropId));
	}
	
	public String getName() {
		NpcTemplate npc = getBossNpc();
		return npc != null ? npc.getName() : "";
	}
	
	public String getIcon() {
		NpcTemplate npc = getBossNpc();
		return npc != null ? npc.getIcon() : "";
	}
	
	public int getCombatPower() {
		return CombatUtils.calcCombatPower(getNpcTeam());
	}
	
	public long getHp() {
		if (hp >= 0) {
			return hp;
		}
		hp = 0;
		NpcTeamTemplate npcTeam = getNpcTeam();
		NpcTemplate boss = npcTeam.getBoss();
		if (boss != null) {
			hp += boss.getMaxHp();
		}
		NpcTemplate[] npcs = npcTeam.getNpcs();
		if (npcs != null) {
			for (NpcTemplate npc : npcs) {
				if (npc != null) {
					hp += npc.getMaxHp();
				}
			}
		}
		return hp;
	}
	
	private NpcTemplate getBossNpc() {
		NpcTeamTemplate npcTeam = getNpcTeam();
		NpcTemplate boss = npcTeam.getBoss();
		if (boss != null) {
			return boss;
		}
		NpcTemplate[] npcs = npcTeam.getNpcs();
		if (npcs != null) {
			for (NpcTemplate npc : npcs) {
				if (npc != null) {
					return npc;
				}
			}
		}
		return null;
	}

	@Override
	public void validate() throws TemplateException {}
}
